﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataFlock.CRM.Engine.Helpers;
using System.ServiceModel.Description;

namespace DataFlock.CRM.Engine.Common
{
    public static class Configuration
    {
        //public class Configurationaa
        //{
        //    public String ServerAddress;
        //    public String OrganizationName;
        //    public Uri DiscoveryUri;
        //    public Uri OrganizationUri;
        //    public Uri HomeRealmUri = null;
        //    public ClientCredentials DeviceCredentials = null;
        //    public ClientCredentials Credentials = null;
        //    public Enumeration.AuthenticationType EndpointType;
        //}

        internal static Enumeration.AuthenticationType AuthenticationType
        {
            get
            {
                Enumeration.AuthenticationType authenticationType = Enumeration.AuthenticationType.ActiveDirectory;

                string authenticationTypeStr = ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.CRM_CONFIG, "AuthenticationType");
                int authenticationTypeInt = Convert.ToInt32(authenticationTypeStr);
                switch (authenticationTypeInt)
                {
                    case 1:
                        {
                            authenticationType = Enumeration.AuthenticationType.LiveId;
                            break;
                        }
                    default:
                        {
                            authenticationType = Enumeration.AuthenticationType.ActiveDirectory;
                            break;
                        }
                }

                return authenticationType;
            }
        }

        internal static Uri DiscoveryServiceUrl
        {
            get
            {
                return new Uri(string.Format("{0}://{1}:{2}/XRMServices/2011/Discovery.svc", Configuration.UrlModifer, Configuration.ServerName, Configuration.ServerPort));
            }
        }

        internal static int ServerPort
        {
            get
            {
                string serverPort = ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.CRM_CONFIG, "ServerPort");
                return Convert.ToInt32(serverPort);
            }
        }

        internal static bool ImpersonateUser
        {
            get
            {
                string impersonateUser = ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.ENGINE_CONFIG, "ImpersonateUser");
                return Convert.ToBoolean(impersonateUser);
            }
        }

        internal static string UserName
        {
            get
            {
                return ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.ENGINE_CONFIG, "UserName");
            }
        }

        internal static string Password
        {
            get
            {
                return ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.ENGINE_CONFIG, "Password");
            }
        }

        internal static string Domain
        {
            get
            {
                return ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.ENGINE_CONFIG, "Domain");
            }
        }
        
        internal static string ServerName
        {
            get
            {
                return ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.CRM_CONFIG, "ServerName");
            }
        }

        internal static string UrlModifer
        {
            get
            {
                string urlModifier = "http";
                string ssl = ConfiguratationHelper.GetConfiguration(ConfiguratationHelper.CRM_CONFIG, "SSL");
                if (!string.IsNullOrWhiteSpace(ssl))
                {
                    if (Convert.ToBoolean(ssl) == true)
                        urlModifier = "https";
                }
                return urlModifier;
            }
        }


    }
}
